package br.usp.ime.dojo.server;

import java.util.ArrayList;
import java.util.List;

import br.usp.ime.dojo.client.DojoRoomDTO;
import br.usp.ime.dojo.client.SessionDTO;
import br.usp.ime.dojo.client.UserDTO;
import br.usp.ime.dojo.core.entities.DojoRoom;
import br.usp.ime.dojo.core.entities.Session;
import br.usp.ime.dojo.core.entities.User;

public class Convert {
	
	public static UserDTO fromUser(User user) {
		UserDTO dto = new UserDTO();
		dto.setName(user.getName());
		return dto;
	}
	
	public static User toUser(UserDTO dto) {
		User user = new User(dto.getName());
		return user;		
	}
	
	public static DojoRoomDTO fromDojoRoom(DojoRoom room) {
		DojoRoomDTO dto = new DojoRoomDTO();
		dto.setName(room.getName());
		dto.setDescription(room.getDescription());
		dto.setLanguage(room.getLanguage());
		dto.setStarted(room.sessionHasStarted());
		dto.setCreator(fromUser(room.getCreator()));
		
		List<UserDTO> users = new ArrayList<UserDTO>();
		for(User user : room.getUsers()){
			users.add(Convert.fromUser(user));
		}
		
		dto.setUsers(users);
		return dto;
	}
	
	public static DojoRoom toDojoRoom(DojoRoomDTO dto) {
		DojoRoom dojoRoom = new DojoRoom(dto.getName(), dto.getDescription());
		return dojoRoom;
	}

	public static SessionDTO fromSession(Session session) {
		SessionDTO sessionDTO = new SessionDTO(session.getUserName(),session.getUserPlace(),session.getId());
		return sessionDTO;
	}

	public static Session toSession(SessionDTO sessionDTO) {
		Session session = new Session(sessionDTO.getUserName(),sessionDTO.getUserPlace(),sessionDTO.getId());
		return session;
	}

}
